// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import VCookie from 'vue-cookie';
import ElementUI from 'element-stm';
/* eslint-disable */
import animate from 'animate.css';
import resource from 'vue-resource';
import App from './App';
import router from './router';
import '../theme/index.css';
import store from './store/index'
import compatibleForIE from './es/compatibleUtils';
import './es/directives.js';

// 引入样式
// import 'vue-easytable/libs/themes-base/index.css'
import './assets/themes-base/index.css'
import {VTable,VPagination} from 'vue-easytable'

// 注册到全局
Vue.component(VTable.name, VTable)
Vue.component(VPagination.name, VPagination)

/* eslint-enable */

Vue.use(ElementUI);
Vue.use(resource);
Vue.use(VCookie);

// set vue-resource-configration;
// nongyin

Vue.http.options.root = `${window.location.protocol}//${window.location.host}/FMC`;
Vue.http.options.xhr = { withCredentials: true };
Vue.http.options.emulateJSON = true;
Vue.http.options.timeout = 120000;
Vue.http.headers.Authorization = 'Basic YXBpOnBhc3N3b3Jk';
Vue.http.interceptors.push((request, next) => {
  request.credentials = true;
  let timeout;
  // 请求超时处理
  if (request.timeout) {
    timeout = setTimeout(() => {
      Vue.prototype.$message.error('请求超时');
    }, request.timeout);
  }
  // 接口报错处理
  next((res) => {
    clearTimeout(timeout);
    if (res.body.msgType === 'E') {
      if (res.body.rspCode !== 'warn') {
        Vue.prototype.$message.error(res.body.rspMsg);
      }
      res.ok = false;
    }
    if (res.body.msgType === 'L') {
      Vue.prototype.$message.error(res.body.rspData);
      router.push('/');
    }
    return res;
  });
});

Vue.directive('focus', {
  // 当被绑定的元素插入到 DOM 中时……
  componentUpdated(el, { value }) {
    // 聚焦元素
    if (value) {
      el.focus();
    }
  },
});

/* eslint-disable */
Vue.directive('loadmore', {
  bind(el, binding) {
    const selectWrap = el.querySelector('.el-table__body-wrapper');
    selectWrap.addEventListener('scroll', () => {
      const sign = 600;
      if (selectWrap.scrollHeight - selectWrap.scrollTop <= (selectWrap.clientHeight + sign)) {
        binding.value();
        // console.log('距离底部小于100了')
        // console.log(vnode.context)
        // // 指令中不能用this关键字
        // vnode.context.getNewData()
      }
    })
  }
})
/* eslint-enable */

compatibleForIE();

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App },
});
